001    package net.sf.xdc;
002    
003    import java.io.File;
004    import java.io.IOException;
005    
006    import org.apache.tools.ant.BuildException;
007    import org.apache.tools.ant.Task;
008    import org.apache.tools.ant.taskdefs.Execute;
009    import org.apache.tools.ant.taskdefs.Javadoc;
010    import org.apache.tools.ant.types.Commandline;
011    import org.apache.tools.ant.types.CommandlineJava;
012    import org.apache.tools.ant.types.Path;
013    import org.apache.tools.ant.types.Reference;
014    
015    /**
016     * The XdcTask is a task for the <a href="http://ant.apache.org">Apache Ant</a>
017     * build system. It resembles the Javadoc task in many of the two systems'
018     * common attributes.
019     *
020     * @author Jens Voß
021     * @since 0.5
022     * @version 0.5
023     */
024    public class XdcTask extends Task {
025    
026      private static String toString(Path path, String separator) {
027        StringBuffer retVal = new StringBuffer();
028        for (int i = 0; i < path.list().length; i++) {
029          if (i > 0) {
030            retVal.append(separator);
031          }
032          retVal.append(path.list()[i]);
033        }
034        return retVal.toString();
035      }
036    
037      private CommandlineJava cmd = new CommandlineJava();
038      private Path classpath;
039      private Path sourcepath;
040      private File destdir;
041      private String subpackages;
042      private boolean notimestamp;
043      private boolean linksource;
044      private boolean defaultexcludes;
045      private String extensions;
046      private String dialect;
047      private File dialects;
048      private File dialectmapping;
049      private File overview;
050      private File helpfile;
051      private boolean author;
052      private boolean nosince;
053      private boolean version;
054      private String windowtitle;
055      private Javadoc.Html doctitle;
056      private Javadoc.Html header;
057      private Javadoc.Html footer;
058      private Javadoc.Html bottom;
059      private boolean reportmissing;
060      private String charset;
061      private String encoding;
062      private String docencoding;
063      private String locale;
064    
065      /**
066       * Setter method for this XdcTask's classpath. Appends the path to
067       * the existing one.
068       *
069       * @param path The new path to be appended
070       */
071      public void setClasspath(Path path) {
072        if (classpath == null) {
073          classpath = path;
074        }
075        else {
076          classpath.append(path);
077        }
078      }
079    
080      /**
081       * Setter method for this XdcTask's classpathref.
082       *
083       * @param r The classpath reference to be set by this method
084       */
085      public void setClasspathRef(Reference r) {
086        createClasspath().setRefid(r);
087      }
088    
089      /**
090       * Getter method for this XdcTask's classpath.
091       *
092       * @return This XdcTask's classpath
093       */
094      public Path createClasspath() {
095        if (classpath == null) {
096          classpath = new Path(getProject());
097        }
098        return classpath.createPath();
099      }
100    
101      /**
102       * Setter method for this XdcTask's source path. Appends the path passed
103       * as parameter to the existing one.
104       *
105       * @param path The new path to be appended to the source path.
106       */
107      public void setSourcepath(Path path) {
108        if (sourcepath == null) {
109          sourcepath = path;
110        }
111        else {
112          sourcepath.append(path);
113        }
114      }
115    
116      /**
117       * Setter method for this XdcTask's source path reference.
118       *
119       * @param r The source path reference to be set by this method
120       */
121      public void setSourcepathRef(Reference r) {
122        createSourcepath().setRefid(r);
123      }
124    
125      /**
126       * Getter method for this XdcTask's source path.
127       *
128       * @return This XdcTask's source path
129       */
130      public Path createSourcepath() {
131        if (sourcepath == null) {
132          sourcepath = new Path(getProject());
133        }
134        return sourcepath.createPath();
135      }
136    
137      /**
138       * Setter method for this XdcTask's destination directory.
139       *
140       * @param destdir The destination directory to be set
141       */
142      public void setDestdir(File destdir) {
143        this.destdir = destdir;
144      }
145    
146      /**
147       * Setter method for this XdcTask's subpackages attribute.
148       *
149       * @param subpackages The subpackages attribute to be set
150       */
151      public void setSubpackages(String subpackages) {
152        this.subpackages = subpackages;
153      }
154    
155      /**
156       * Setter method for this XdcTask's notimestamp attribute.
157       *
158       * @param notimestamp The notimestamp attribute to be set
159       */
160      public void setNotimestamp(boolean notimestamp) {
161        this.notimestamp = notimestamp;
162      }
163    
164      /**
165       * Setter method for this XdcTask's linksource attribute.
166       *
167       * @param linksource The linksource attribute to be set
168       */
169      public void setLinksource(boolean linksource) {
170        this.linksource = linksource;
171      }
172    
173      /**
174       * Setter method for this XdcTask's defaultexcludes attribute.
175       *
176       * @param defaultexcludes The defaultexcludes attribute to be set
177       */
178      public void setDefaultexcludes(boolean defaultexcludes) {
179        this.defaultexcludes = defaultexcludes;
180      }
181    
182      /**
183       * Setter method for this XdcTask's extensions attribute.
184       *
185       * @param extensions The extensions attribute to be set
186       */
187      public void setExtensions(String extensions) {
188        this.extensions = extensions;
189      }
190    
191      /**
192       * Setter method for this XdcTask's dialect attribute.
193       *
194       * @param dialect The dialect attribute to be set
195       */
196      public void setDialect(String dialect) {
197        this.dialect = dialect;
198      }
199    
200      /**
201       * Setter method for this XdcTask's dialects attribute.
202       *
203       * @param dialects The dialects attribute to be set
204       */
205      public void setDialects(File dialects) {
206        this.dialects = dialects;
207      }
208    
209      /**
210       * Setter method for this XdcTask's dialectmapping attribute.
211       *
212       * @param dialectmapping The dialectmapping attribute to be set
213       */
214      public void setDialectmapping(File dialectmapping) {
215        this.dialectmapping = dialectmapping;
216      }
217    
218      /**
219       * Setter method for this XdcTask's overview attribute.
220       *
221       * @param overview The overview attribute to be set
222       */
223      public void setOverview(File overview) {
224        this.overview = overview;
225      }
226    
227      /**
228       * Setter method for this XdcTask's helpfile attribute.
229       *
230       * @param helpfile The helpfile attribute to be set
231       */
232      public void setHelpfile(File helpfile) {
233        this.helpfile = helpfile;
234      }
235    
236      /**
237       * Setter method for this XdcTask's author attribute.
238       *
239       * @param author The author attribute to be set
240       */
241      public void setAuthor(boolean author) {
242        this.author = author;
243      }
244    
245      /**
246       * Setter method for this XdcTask's nosince attribute.
247       *
248       * @param nosince The nosince attribute to be set
249       */
250      public void setNosince(boolean nosince) {
251        this.nosince = nosince;
252      }
253    
254      /**
255       * Setter method for this XdcTask's version attribute.
256       *
257       * @param version The version attribute to be set
258       */
259      public void setVersion(boolean version) {
260        this.version = version;
261      }
262    
263      /**
264       * Setter method for this XdcTask's windowtitle attribute.
265       *
266       * @param windowtitle The windowtitle attribute to be set
267       */
268      public void setWindowtitle(String windowtitle) {
269        this.windowtitle = windowtitle;
270      }
271    
272      /**
273       * Setter method for this XdcTask's doctitle attribute.
274       *
275       * @param doctitle The doctitle attribute to be set
276       */
277      public void setDoctitle(String doctitle) {
278        Javadoc.Html html = new Javadoc.Html();
279        html.addText(doctitle);
280        addDoctitle(html);
281      }
282    
283      /**
284       * Alternative setter method for this XdcTask's doctitle attribute.
285       *
286       * @param doctitle The doctitle (in HTML format) to be set
287       */
288      public void addDoctitle(Javadoc.Html doctitle) {
289        this.doctitle = doctitle;
290      }
291    
292      /**
293       * Setter method for this XdcTask's header attribute.
294       *
295       * @param header The header attribute to be set
296       */
297      public void setHeader(String header) {
298        Javadoc.Html html = new Javadoc.Html();
299        html.addText(header);
300        addHeader(html);
301      }
302    
303      /**
304       * Alternative setter method for this XdcTask's header attribute.
305       *
306       * @param header The header (in HTML format) to be set
307       */
308      public void addHeader(Javadoc.Html header) {
309        this.header = header;
310      }
311    
312      /**
313       * Setter method for this XdcTask's footer attribute.
314       *
315       * @param footer The footer attribute to be set
316       */
317      public void setFooter(String footer) {
318        Javadoc.Html html = new Javadoc.Html();
319        html.addText(footer);
320        addFooter(html);
321      }
322    
323      /**
324       * Alternative setter method for this XdcTask's footer attribute.
325       *
326       * @param footer The footer (in HTML format) to be set
327       */
328      public void addFooter(Javadoc.Html footer) {
329        this.footer = footer;
330      }
331    
332      /**
333       * Setter method for this XdcTask's bottom attribute.
334       *
335       * @param bottom The bottom attribute to be set
336       */
337      public void setBottom(String bottom) {
338        Javadoc.Html html = new Javadoc.Html();
339        html.addText(bottom);
340        addBottom(html);
341      }
342    
343      /**
344       * Alternative setter method for this XdcTask's bottom attribute.
345       *
346       * @param bottom The bottom (in HTML format) to be set
347       */
348      public void addBottom(Javadoc.Html bottom) {
349        this.bottom = bottom;
350      }
351    
352      /**
353       * Setter method for this XdcTask's reportmissing attribute.
354       *
355       * @param reportmissing The reportmissing attribute to be set
356       */
357      public void setReportmissing(boolean reportmissing) {
358        this.reportmissing = reportmissing;
359      }
360    
361      /**
362       * Getter method for a new argument to be passed to this XdcTask.
363       *
364       * @return A new argument for passing to this XdcTask.
365       */
366      public Commandline.Argument createArg() {
367        return cmd.createArgument();
368      }
369    
370      /**
371       * Getter method for a new Java VM argument to be passed to this XdcTask.
372       *
373       * @return A new Java VM argument for passing to this XdcTask.
374       */
375      public Commandline.Argument createJvmarg() {
376        return cmd.createVmArgument();
377      }
378    
379      /**
380       * Setter method for this XdcTask's charset attribute. This may be the
381       * name or the alias of any known character set.
382       *
383       * @param charset The charset name or alias to be set
384       */
385      public void setCharset(String charset) {
386        this.charset = charset;
387      }
388    
389      /**
390       * Setter method for this XdcTask's encoding attribute. This may be the
391       * name or the alias of any known character set.
392       *
393       * @param encoding The encoding name or alias to be set
394       */
395      public void setEncoding(String encoding) {
396        this.encoding = encoding;
397      }
398    
399      /**
400       * Setter method for this XdcTask's docencoding attribute. This may be the
401       * name or the alias of any known character set.
402       *
403       * @param docencoding The docencoding name or alias to be set
404       */
405      public void setDocencoding(String docencoding) {
406        this.docencoding = docencoding;
407      }
408    
409      /**
410       * Setter method for this XdcTask's locale attribute.
411       *
412       * @param locale The locale name to be set
413       */
414      public void setLocale(String locale) {
415        this.locale = locale;
416      }
417    
418      /**
419       * This method executes this XdcTask.
420       *
421       * @throws BuildException
422       */
423      public void execute() throws BuildException {
424        try {
425          CommandlineJava cmd2 = (CommandlineJava) cmd.clone();
426          cmd2.setClassname("net.sf.xdc.Main");
427    
428          if (sourcepath != null) {
429            cmd2.createArgument().setValue("-sourcepath");
430            cmd2.createArgument().setValue(toString(sourcepath, ";"));
431          }
432          if (destdir != null) {
433            cmd2.createArgument().setValue("-d");
434            cmd2.createArgument().setFile(destdir);
435          }
436          if (subpackages != null) {
437            cmd2.createArgument().setValue("-subpackages");
438            cmd2.createArgument().setValue(subpackages);
439          }
440          if (notimestamp) {
441            cmd2.createArgument().setValue("-notimestamp");
442          }
443          if (linksource) {
444            cmd2.createArgument().setValue("-linksource");
445          }
446          if (defaultexcludes) {
447            cmd2.createArgument().setValue("-defaultexcludes");
448          }
449          if (extensions != null) {
450            cmd2.createArgument().setValue("-extensions");
451            cmd2.createArgument().setValue(extensions);
452          }
453          if (dialect != null) {
454            cmd2.createArgument().setValue("-dialect");
455            cmd2.createArgument().setValue(dialect);
456          }
457          if (dialects != null) {
458            cmd2.createArgument().setValue("-dialects");
459            cmd2.createArgument().setFile(dialects);
460          }
461          if (dialectmapping != null) {
462            cmd2.createArgument().setValue("-dialectmapping");
463            cmd2.createArgument().setFile(dialectmapping);
464          }
465          if (overview != null) {
466            cmd2.createArgument().setValue("-overview");
467            cmd2.createArgument().setFile(overview);
468          }
469          if (helpfile != null) {
470            cmd2.createArgument().setValue("-helpfile");
471            cmd2.createArgument().setFile(helpfile);
472          }
473          if (author) {
474            cmd2.createArgument().setValue("-author");
475          }
476          if (nosince) {
477            cmd2.createArgument().setValue("-nosince");
478          }
479          if (version) {
480            cmd2.createArgument().setValue("-version");
481          }
482          if (windowtitle != null) {
483            cmd2.createArgument().setValue("-windowtitle");
484            cmd2.createArgument().setValue(windowtitle);
485          }
486          if (doctitle != null) {
487            cmd2.createArgument().setValue("-doctitle");
488            cmd2.createArgument().setValue(expand(doctitle.getText()));
489          }
490          if (header != null) {
491            cmd2.createArgument().setValue("-header");
492            cmd2.createArgument().setValue(expand(header.getText()));
493          }
494          if (footer != null) {
495            cmd2.createArgument().setValue("-footer");
496            cmd2.createArgument().setValue(expand(footer.getText()));
497          }
498          if (bottom != null) {
499            cmd2.createArgument().setValue("-bottom");
500            cmd2.createArgument().setValue(expand(bottom.getText()));
501          }
502          if (reportmissing) {
503            cmd2.createArgument().setValue("-reportmissing");
504          }
505          if (charset != null) {
506            cmd2.createArgument().setValue("-charset");
507            cmd2.createArgument().setValue(charset);
508          }
509          if (encoding != null) {
510            cmd2.createArgument().setValue("-encoding");
511            cmd2.createArgument().setValue(encoding);
512          }
513          if (docencoding != null) {
514            cmd2.createArgument().setValue("-docencoding");
515            cmd2.createArgument().setValue(docencoding);
516          }
517          if (locale != null) {
518            cmd2.createArgument().setValue("-locale");
519            cmd2.createArgument().setValue(locale);
520          }
521    
522          String[] args = cmd2.getCommandline();
523          String[] args2 = new String[args.length + 2];
524          args2[0] = args[0];
525          args2[1] = "-classpath";
526          args2[2] = classpath.toString();
527          System.arraycopy(args, 1, args2, 3, args.length - 1);
528    
529          Execute exe = new Execute();
530          exe.setCommandline(args2);
531    
532          if (exe.execute() != 0) {
533            throw new BuildException("XDC task failed");
534          }
535        }
536        catch (IOException e) {
537          throw new BuildException(e);
538        }
539        catch (CloneNotSupportedException e) {
540          throw new BuildException(e);
541        }
542      }
543    
544      private String expand(String content) {
545          return getProject().replaceProperties(content);
546      }
547    
548    }